<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>view.cpp Example File (mandelbrot/view.cpp)</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <script src="./scripts/superfish.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />  <script src="./scripts/narrow.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->
</head>
<body class="" onload="CheckEmptyAndLoadList();">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="150"><img src="images/qt-logo.png" align="left" border="0" />&nbsp;&nbsp;<img src="images/qtopencl-logo.png" border="0" /></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"> <a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="qtopencl-all-classes.html"><font color="#004faf">All Classes</font></a></td>
</table><h1 class="title">view.cpp Example File</h1>
<span class="small-subtitle">mandelbrot/view.cpp</span>
<!-- $$$mandelbrot/view.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="highlightedCode brush: cpp"><span class="comment">    /****************************************************************************
    **
    ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    ** All rights reserved.
    ** Contact: Nokia Corporation (qt-info@nokia.com)
    **
    ** This file is part of the QtOpenCL module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:LGPL$
    ** No Commercial Usage
    ** This file contains pre-release code and may not be distributed.
    ** You may use this file in accordance with the terms and conditions
    ** contained in the Technology Preview License Agreement accompanying
    ** this package.
    **
    ** GNU Lesser General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU Lesser
    ** General Public License version 2.1 as published by the Free Software
    ** Foundation and appearing in the file LICENSE.LGPL included in the
    ** packaging of this file.  Please review the following information to
    ** ensure the GNU Lesser General Public License version 2.1 requirements
    ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    **
    ** In addition, as a special exception, Nokia gives you certain additional
    ** rights.  These rights are described in the Nokia Qt LGPL Exception
    ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    **
    ** If you have questions regarding the use of this file, please contact
    ** Nokia at qt-info@nokia.com.
    **
    **
    **
    **
    **
    **
    **
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/</span>

    #include &quot;view.h&quot;
    #include &quot;image.h&quot;
    #include &quot;palette.h&quot;
    #include &quot;zoom.h&quot;
    #include &lt;QtGui/qpainter.h&gt;
    #include &lt;QtGui/qevent.h&gt;
    #include &lt;QtCore/qtimer.h&gt;

    View::View(QWidget *parent)
        : QWidget(parent)
    {
        setAttribute(Qt::WA_OpaquePaintEvent);

        palette = new Palette();
        palette-&gt;setStandardPalette(Palette::EarthSky);
        offset = 0.0f;
        step = 0.005f;

        zoom = new GoldenGradientZoom();

        image = 0;

        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(animate()));
        timer-&gt;start(0);

        resizeTimer = new QTimer(this);
        resizeTimer-&gt;setSingleShot(true);
        connect(resizeTimer, SIGNAL(timeout()), this, SLOT(performResize()));
        resizeTimer-&gt;start(0);
        firstResize = true;
    }

    View::~View()
    {
        delete palette;
        delete image;
        delete zoom;
    }

    void View::paintEvent(QPaintEvent *)
    {
        if (!image)
            return;
        QPainter painter(this);
        QSize imgSize = image-&gt;size();
        int x = (width() - imgSize.width()) / 2;
        int y = (height() - imgSize.height()) / 2;
        image-&gt;paint(&amp;painter, QPoint(x, y));
        if (timer-&gt;isActive()) {
            qreal fps = frameRate.fps();
            if (fps &gt; 0.0f) {
                QString str = QString::number(fps) + QLatin1String(&quot; fps&quot;);
                painter.setPen(Qt::white);
                painter.drawText(rect(), str);
            }
        }
    }

    void View::keyPressEvent(QKeyEvent *event)
    {
        if (event-&gt;key() == Qt::Key_Space) {
            if (timer-&gt;isActive()) {
                timer-&gt;stop();
                frameRate.stop();
            } else {
                timer-&gt;start();
                frameRate.start();
            }
            update();
        }
        QWidget::keyPressEvent(event);
    }

    void View::resizeEvent(QResizeEvent *event)
    {
        if (!firstResize)
            resizeTimer-&gt;start(100);
        QWidget::resizeEvent(event);
    }

    void View::animate()
    {
        if (!image)
            return;
        if (step &gt; 0) {
            offset += step;
            if (offset &gt;= 1.0f) {
                offset = 1.0f;
                step = -step;
            }
        } else {
            offset += step;
            if (offset &lt;= 0.0f) {
                offset = 0.0f;
                step = -step;
            }
        }
        zoom-&gt;generate(image, offset, *palette);
        update();
        frameRate.newFrame();
    }

    void View::performResize()
    {
        firstResize = false;

        int wid = (width() + 15) &amp; ~15;
        int ht = (height() + 15) &amp; ~15;

        if (!image || image-&gt;size() != QSize(wid, ht)) {
            delete image;
            image = Image::createImage(wid, ht);
            image-&gt;initialize();

            frameRate.start();

            animate();
        }
    }</pre>
</div>
<!-- @@@mandelbrot/view.cpp -->
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td align="left">Copyright &copy; 2010 Nokia Corporation</td>
<td align="right">QtOpenCL Documentation</td>
</tr></table></div></address>  <script src="scripts/functions.js" type="text/javascript"></script>
  <script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-4457116-5']);
  _gaq.push(['_trackPageview']);
  (function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  </script>
</body>
</html>
